<?php

namespace app\server;

use think\admin\storage\QiniuStorage;

class QnServer
{
    /**
     * 七牛云上传
     *
     * type是对应的上传目录
     * @param $path
     * @param string $type
     * @throws \think\admin\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function upload($url, $type = 'default')
    {
        preg_match('/upload\/(.*)/', $url, $preg);
        $path = $preg[0];
        $key = $preg[1];
        $result = QiniuStorage::instance()->set($type.'/upload/'.$key, file_get_contents('./'.$path), 0);
//        $result = json_decode($result, true);
        if (isset($result['uploaded']) && $result['uploaded'] === true) {
           $url = $result['url'];
           edit('system_file',['xkey'=>$key],['xurl'=>$url]);
        }
        return $url;
    }

    /**
     * 小鯨魚故事音频上传七牛云
     * @param $url
     * @param $type
     * @param $storyId
     * @throws \think\admin\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function whaleStoryVideo($url,$type,$storyId){
        $key = 'story_'.$storyId.".mp3";
        $result = QiniuStorage::instance()->set($type.'/upload/'.$key, file_get_contents($url), 0);
        if (isset($result['uploaded']) && $result['uploaded'] === true) {
            $url = $result['url'];
            edit('app_whale_story',['id'=>$storyId],['video_link'=>$url]);
            edit('app_whale_story_video',['story_id'=>$storyId],['cdn_url'=>$url]);
        }
    }


}

?>